/**
 * Groovy集合&数组
 * https://www.jianshu.com/p/5a76a907aed9
 */
class TestMap {

    public static void main(String[] args) {

        def map1 = [name: "xq", age: 26]

        //Groovy 中定义的 Map 默认类型是 java.util.LinkedHashMap
        println map1.getClass()//class java.util.LinkedHashMap
        println()


        //Map 集合中指定 key 下的值有有两种方式：
        //
        //    map.get(key)
        //    map[key]
        //    map.key
        println "the name is ${map1.get("name")} and age is ${map1.get("age")}"
        println "the name is ${map1['name']} and age is ${map1['age']}"
        println "the name is ${map1.name} and age is ${map1.age}"
        //获取一个不存的key对应值,那么会得到null
        println map1.top//null


        println()
        //添加
        map1.level = 'middle'
        println "the name is ${map1['name']} and age is ${map1['age']} and level is ${map1['level']}"


        println()
        //修改
        map1['age'] = 25
        println "the name is ${map1['name']} and age is ${map1['age']}"


        println()
        //不带角标的遍历
        map1.each { key, value ->
            println key + "-" + value
        }

        println()
        //带有角标的遍历
        map1.eachWithIndex { Map.Entry entry, int i ->

            //name-六号表哥 index = 0
            //age-26 index = 1
            println entry.key + "-" + entry.value + " index = " + i//age-26 index = 1
        }


        println()

    }

}
